package com.zhao.Util;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class IPUtil {
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "本地";
        }
        return ip;
    }

    /**
     * 获取指定间距的时间日期
     * @param before 获取几天
     * @param numDay 间隔几天
     * @return
     */
    public static List<String> before7Day(int before,int numDay){
        List<String> day=new ArrayList<>();
        Calendar cal=Calendar.getInstance();
        for (int i = 0; i <before ; i++) {

            int y=cal.get(Calendar.YEAR);
            int m=cal.get(Calendar.MONTH)+1;
            int d=cal.get(Calendar.DATE);
            day.add(y+"-"+m+"-"+d);
            cal.add(Calendar.DATE, -numDay);
        }
        Collections.reverse(day);
        return day;
    }
    public static String month(){
        Calendar cal=Calendar.getInstance();
            int y=cal.get(Calendar.YEAR);
            int m=cal.get(Calendar.MONTH)+1;
        return y+"-"+m+"-01";
    }
    public static List<String> dayToDay(String start) throws ParseException {
        List<String> day=new ArrayList<>();
        //int i = beforePresentDay(start);//距今多少天

        //如果 小于 一年 按月计算
        //    小于 五年按年
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date star =dft.parse(start);//开始时间;
        Calendar cal=Calendar.getInstance();
        cal.setTime(star);
        Calendar cal1=Calendar.getInstance();
        String now=cal1.get(Calendar.YEAR)+"-"+cal1.get(Calendar.MONTH)+1;//当前年月
        while (true){
            String old=cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+1;
            if (old.equals(now)){
                break;
            }
            int y=cal.get(Calendar.YEAR);
            int m=cal.get(Calendar.MONTH)+1;
            int d=cal.get(Calendar.DATE);
            day.add(y+"-"+m+"-"+d);
            System.out.println(y+"-"+m+"-"+d);
            cal.add(Calendar.MONTH, 1);
        }
        return day;
    }

    /**
     * 获取 传入时间到今日 共多少天
     * @param date 时间（格式yyyy-MMM-dd）
     * @return
     */
    private static int beforePresentDay(String date){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Long num=0L;
        try {
            Date star =dft.parse(date);//开始时间;
            Date endDay=new Date();//结束时间
            Long starTime=star.getTime();
            Long endTime=endDay.getTime();
            num=endTime-starTime;//时间戳相差的毫秒数
            //  System.out.println("相差天数为："+num/24/60/60/1000);//除以一天的毫秒数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int)(num/24/60/60/1000);
    }

}
